﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using StockManagement.Commands;
using StockManagement.Helper;
using StockManagement.Models;
using System.Windows.Data;
using System.Windows;

namespace StockManagement.ViewModels
{
    public class MaterialGridViewModel : BaseViewModel
    {
        ObservableCollection<MaterialViewModel> _allMaterialCollection;
        ObservableCollection<VendorViewModel> _allVendorCollection;
        ObservableCollection<MaterialViewModel> _materialCollection;
        DelegateCommand _ExportToExcelCommand;

        public MaterialGridViewModel(ObservableCollection<MaterialViewModel> materialCollection,ObservableCollection<VendorViewModel> vendorcoll)
        {
            PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(MaterialGridViewModel_PropertyChanged);
            _allMaterialCollection = materialCollection;
            _allVendorCollection = vendorcoll;
            _allMaterialCollection.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_allMaterialCollection_CollectionChanged);
            _materialCollection = new ObservableCollection<MaterialViewModel>();
            ShowMaterial = DGShowMaterial.All;
            _ExportToExcelCommand = new DelegateCommand(ExecuteExportToExcel);            
        }

        void MaterialGridViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "ShowMaterial":
                    SetMaterialCollection();
                    break;
            }
        }

        internal void RaisePropertyChanged()
        {
            OnPropertyChanged("ShowMaterial");
        }

        private void SetMaterialCollection()
        {
            MaterialCollection.Clear();
            switch (ShowMaterial)
            {
                case DGShowMaterial.All:
                    var coll = AllMaterialCollection.AsEnumerable();
                    MaterialCollection = new ObservableCollection<MaterialViewModel>(coll);
                    ShowPurchaser = Visibility.Visible;
                    break;
                case DGShowMaterial.Trading:
                    coll = AllMaterialCollection.Where(p => p.MaterialType == Helper.EnumMaterialType.Trading);
                    MaterialCollection = new ObservableCollection<MaterialViewModel>(coll);
                    ShowPurchaser = Visibility.Visible;
                    break;
                case DGShowMaterial.Manufacturing:
                    ShowPurchaser = Visibility.Collapsed;
                    coll = AllMaterialCollection.Where(p => p.MaterialType == Helper.EnumMaterialType.Manufacturing);
                    MaterialCollection = new ObservableCollection<MaterialViewModel>(coll);
                    break;                
            }
        }

        void _allMaterialCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                    if (e.NewItems == null || e.NewItems.Count < 1)
                        return;

                    var matVM = e.NewItems[0] as MaterialViewModel;
                    if (matVM == null)
                        return;

                    var item = MaterialCollection.FirstOrDefault(p => p.MaterialID == matVM.MaterialID);
                    if (item != null)
                        return;

                    if((ShowMaterial == DGShowMaterial.Manufacturing && matVM.MaterialType == EnumMaterialType.Manufacturing) ||
                        (ShowMaterial == DGShowMaterial.Trading && matVM.MaterialType == EnumMaterialType.Trading) ||
                        ShowMaterial == DGShowMaterial.All)
                        MaterialCollection.Add(matVM);

                    break;
                case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                    if (e.OldItems == null || e.OldItems.Count < 1)
                        return;
                    matVM = e.OldItems[0] as MaterialViewModel;
                    if (matVM == null)
                        return;

                    item = MaterialCollection.FirstOrDefault(p => p.MaterialID == matVM.MaterialID);
                    if (item == null)
                        return;

                    MaterialCollection.Remove(item);
                    break;
            }
        }

        DGShowMaterial _shwMaterial;
        public DGShowMaterial ShowMaterial
        {
            get { return _shwMaterial; }
            set
            {
                _shwMaterial = value;
                OnPropertyChanged("ShowMaterial");
            }
        }

        Visibility _ShowPurchaser;
        public Visibility ShowPurchaser
        {
            get { return _ShowPurchaser; }
            set
            {
                _ShowPurchaser = value;
                OnPropertyChanged("ShowPurchaser");
            }
        }

        public ObservableCollection<MaterialViewModel> AllMaterialCollection
        {
            get
            {
                return _allMaterialCollection;
            }
        }

        public ObservableCollection<MaterialViewModel>MaterialCollection
        {
            get { return _materialCollection; }
            set 
            {
                _materialCollection = value;
                OnPropertyChanged("MaterialCollection");
            }
        }

        public DelegateCommand ExportToExcelCommand
        {
            get
            {
                return _ExportToExcelCommand;
            }
        }

        private void ExecuteExportToExcel(object item)
        {
            ExportToExcel<ExcelMaterial, List<ExcelMaterial>> exportToExcel = new ExportToExcel<ExcelMaterial, List<ExcelMaterial>>();
            exportToExcel.dataToPrint = MaterialCollection.Select(p => new ExcelMaterial()
            {
                Name = p.MaterialName,
                TotalPrice = p.TotalPrice,
                MaterialCode = p.MaterialCode,
                MaterialType = p.MaterialType,
                MaterialRate = p.MaterialRate,
                Vendor = _allVendorCollection.FirstOrDefault(q=>q.VendorID == p.VendorID).CompleteName,
                Quantity = p.Quantity,
            }).ToList();
            exportToExcel.GenerateReport();
        }
    }
}
